package cn.itcast.solrdemo;

import java.io.IOException;import java.util.Iterator;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

public class SolrIndexCreateTest {
	@Test
	public void testCreateIndex() throws SolrServerException, IOException{
		//连接服务器
		SolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
		//创建solr的输入document
		SolrInputDocument document = new SolrInputDocument();
		//添加字段
		document.addField("id", "100");
		document.addField("title", "秋名山车神");
		document.addField("price", 18888888);
		//添加document到srever中
		server.add(document);
		//提交请求
		server.commit();
	}
	/**
	 * 删除
	 * @throws SolrServerException
	 * @throws IOException
	 */
	@Test
	public void testDeleteIndex() throws SolrServerException, IOException{
		//连接服务器
		SolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
		server.deleteById("101");
		//提交请求
		server.commit();
	}
	/**
	 * 根据查询条件删除
	 * @throws SolrServerException
	 * @throws IOException
	 */
	@Test
	public void testDeleteIndexByQuery() throws SolrServerException, IOException{
		//连接服务器
		SolrServer server = new HttpSolrServer("http://localhost:8080/solr/core2");
		server.deleteByQuery("title:iphone");
		//提交请求
		server.commit();
	}
	@Test
	public void testQueryDocument() throws Exception{
		//连接Solr服务器
		SolrServer server =  new HttpSolrServer("http://localhost:8080/solr/core2");
		//创建查询对象
		SolrQuery solrQuery = new SolrQuery("title:手机");
		//执行查询
		QueryResponse response = server.query(solrQuery);
		
		//获取查询结果,本质是一个Document对象
		SolrDocumentList results = response.getResults();
		
		System.out.println("本次共搜索到"+results.size()+"条数据");
		
		for (SolrDocument document : results) {
			
			System.out.println("id:"+document.getFieldValue("id"));
			System.out.println("id:"+document.getFieldValue("title"));
			System.out.println("id:"+document.getFieldValue("price"));
		}
		
	}
	/**
	 * 给solr云添加数据
	 * @throws Exception
	 */
	@Test
	public void testWriter() throws Exception{
		//创建SolrServer
		CloudSolrServer server = new CloudSolrServer("192.168.206.101:2181,192.168.206.102:2181,192.168.206.103:2181");
		//指定要访问的Collection名称
		server.setDefaultCollection("myCollection2");
		//创键Document对象
		SolrInputDocument document = new SolrInputDocument();
		//添加字段
		document.addField("id", "20");
		document.addField("title", "duang手机,自带特效");
		//添加Document到Server
		server.add(document);
		//提交
		server.commit();
				
	}
	/**
	 * 删除数据
	 * @throws SolrServerException
	 * @throws IOException
	 */
	@Test
	public void tsetDelete() throws SolrServerException, IOException{
		//创建solrServer
		CloudSolrServer server = new CloudSolrServer("192.168.206.101:2181,192.168.206.102:2181,192.168.206.103:2181");
		//指定要访问的Collection名称
		server.setDefaultCollection("myCollection2");
		//根据id删除
		server.deleteById("20");
		//提交
		server.commit();
	}
	
	/**
	 * 查询solr
	 * @throws Exception
	 */
	@Test
	public void testSearch() throws Exception{
		//创建SolrServer
		CloudSolrServer server = new CloudSolrServer("192.168.206.101:2181,192.168.206.102:2181,192.168.206.103:2181");
		//指定要访问的Collection
		server.setDefaultCollection("myCollection2");
		//开始查询数据
		QueryResponse response = server.query(new SolrQuery("title:手机"));
		//以Document形式解析
		SolrDocumentList documentList = response.getResults();
		//遍历
		for (SolrDocument solrDocument : documentList) {
			System.out.println(solrDocument.get("id"));
			System.out.println(solrDocument.get("title"));
		}
		
	}
}
